package com.zky.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zky.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016101100658666";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCdPEwKyoHcvXc2zT7ppD88FvA/95VyEM74YlxR2FCwCDmGZV58i2YaXzwgZXaXHbouPVVVnmZE6QcXhtMcl5stEAydIrRvBKs6qa2ytuAaCJSfaFu85iacC5238qYeBNRiP6t5ozVCeGS4u1WzZ+67sR/EiqDNa9aR2anjnU2li5lCWRZSAa1nt5zzHL+6EK6Gix+eG4PYVjaIG81pkdA2oyA4JdpnfVaFakpPZYceDUO2tDQTqTuo/h9iofwp2RFq+bvnBJhv5Ds/nxpW85/E/VQlKJLmHtYGZZfTSlsZYLd38gn3YnIJewnegd9oZrHlpFXUWuKPs4HlSFRDSPFNAgMBAAECggEAC5GiLrETuQAfw2J6nIo/NNajOrGmx0vBTrk9/a+i0DDqup6+wv8mtg4HyIJupVPEnR5aJwCOmcdjhDGhWts0ya4Cn5UsHXcEej59z0ZxWrkC5m+xQEbW1GhuZFhwLIlCbzAsF7kQdCGTl5wBBEBDan/vzHA+wJ6MnRKc02iBjAqJtnZNDNwQ/fNpNgLHZkCg+bvrcl/8HytgCYFXt/wOf8ecQSl45lv7yG8BoqFy0eTJsJVbxSu9jkgkBUeErCnjW3QUXr4jpM76t0kyLnzgLHTsN19SbsgEReunaU8DcYNbE7u5lks4EmXTht+K5QUJ0nd102MWvn7Wfvndm4EHwQKBgQDg/Xn9RajMAxC1Ht4ohITUrvGK540/Di4dvbpx4Xx95Kbl4ii54i4tCFI89M1ga1f0bMSHX6uAE9DS1+24OJYZBB+9XjvOG/nzxjU8IEGcTmbGPb8PrNOwQCyxw5i+pCau2MlTMAsWFZ46z7SAnMZQ6n1Xach9i0DK2eNyn+cdRQKBgQCy6Cz0JGQJhz7JmTV9kKWxlKizs7gTx4dh5t6VwQH4m1mO65xkza1WD+Yk1sR1GAZY4N5ZN64qU8lC6QQ4tEjV7Z+1+D8N6gVGL5+EZvN90WuEA3fbdDMYSvN+D1pKkK9UzRenszr9XdXpP0UMKFM/WSjjuFpuRvBhJ55mZY0waQKBgQDV5eXuNiWuH1Po/bmzx5kJInEc39rXEcIiIFh7ICZdAd7yOrsBTdkckwasUQhYHHV4OVf2Y6wGjdBAQ20LcTCyA8uxGz+8SbiuW8gMq5X05kyrN9THMEXpEHntXsl+pUzpecydO9WuX0sZGpK+m02na+dSDY3AXX1x3Qhx1zfACQKBgA5YbZpZ/KeC77bskQxzd8XIfMqEA6OaYTGpeHmbk1Pi6Qh7vfubPmXjOnoDupcFCSVBSrLuswlz1HSICK2fTyPQPPkzYaL0+91H3kuYkbep/iwEtyyioaI2CFY4Vu4ZseVj2Jy602UTjB5600eJSDuRI6U22BF1oLq5mYruY6bZAoGAUrnyhkIlOgaDv4043G6jpKWYRVLLj+g+9Blw/g16xAUY0TcRqL6TA2P0ijkaBq8QtoqdsTqJ0hIJ1SmnM3BcoEpWeSRzK1wuKiSWexjQW52he6R/LCC2ekPtuS0i8LGXqYD7IPLOfCXFyd0i7MNsKsyqy62FiRrvMwCbQrKNlz0=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmTixFPnKaPUzWyHGJswTHhB8g8kd7VHpCv/Vaj2rKA8JvN+ymY7Qiz2nV4Z5nRI8CLeqXCsgmB0ajl4G/yyXQX/Mg9RcK3hkqp01LvjYTtzjHG0a3GUfWpVy8/sIA79CYvibVaiDeUBzKIFaQfjN2nu42euXtEOY17xWocjdFFX1qaTcshHLzs0oims8euhI7wVinr+xdGu3xZnAZSz21vlNnd38tvdilLfnbwPdNSFE5E/SFrw3vtFyg/xwg9uss6w34mZB6+TNx1K1MVUroNGqHxTNcPZ5wEUxTUKSObTUwYidKBO//T1TQsojCcljWqkYklauYOaBiaihSHIXewIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://zkypaycallback.free.idcfengye.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.zky.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝支付自动收单时间，超时未操作
    private  String timeOut = "3m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeOut +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
